﻿using Demo.Domains.Entities.Sys;
using Demo.PetaPoco.Application.DbContexts;
using Demo.PetaPoco.Application.Dtos.Dept;
using Demo.PetaPoco.Application.Services.Area;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using PmSoft.Core;
using PmSoft.Data.Abstractions;

namespace Demo.PetaPoco.Application.Services.Dept;
[Inject(ServiceLifetime.Scoped)]
public class DeptService
{
	private readonly DeptRepository deptRepository;
	private readonly AreaService areaService;
	private readonly ICachedEntityLoader areaLoader;

	public DeptService(DeptRepository deptRepository, AreaService areaService,
		ICachedEntityLoader<AppDbContext> areaLoader)
	{
		this.deptRepository = deptRepository;
		this.areaService = areaService;
		this.areaLoader = areaLoader;
	}


	public async Task<DeptDetailDto> GetDeptDetailAsync(int deptId)
	{
		var dept = await deptRepository.GetAsync(deptId);
		if (dept == null)
			throw new PmSoftException("部门不存在");
		return dept.Adapt<DeptDetailDto>();
	}

	public async Task<IEnumerable<DeptDetailDto>> GetDeptsAsync(IEnumerable<int> deptIds)
	{
		var depts = await deptRepository.GetEntitiesByIdsAsync(deptIds);
		return depts.Adapt<DeptDetailDto[]>();
	}

	public async Task<IPagedList<DeptDto>> GetPagedListAsync(DeptPageQuery query)
	{
		var depts = await deptRepository.GetPagedListAsync(query);
		return await depts.ToPagedDto(dept => dept.MapTo<SysDept, DeptDto>())
			.WithRelatedAsync(
			foreignKeySelector: m => m.ProvinceNum,
			loadRelatedEntities: areaLoader.GetEntitiesByIdsAsync<SysArea, int>,
			attachAction: (dept, area) => dept.ProvinceName = area.Name
			)
			.WithRelatedAsync(
			foreignKeySelector: m => m.CityNum,
			loadRelatedEntities: areaLoader.GetEntitiesByIdsAsync<SysArea, int>,
			attachAction: (dept, area) => dept.CityName = area.Name
			)
			.WithRelatedAsync(
			foreignKeySelector: m => m.RegionNum,
			loadRelatedEntities: areaLoader.GetEntitiesByIdsAsync<SysArea, int>,
			attachAction: (dept, area) => dept.RegionName = area.Name
			);
	}

	public async Task CreateAsync(DeptCreateModel model)
	{
		var areas = await areaService.ParseAreasAsync(model);
		var dept = model.MapTo<DeptCreateModel, SysDept>();
		dept.ProvinceNum = areas.Province?.Id;
		dept.CityNum = areas.City?.Id;
		dept.RegionNum = areas.County?.Id;
		await deptRepository.InsertAsync(dept);
	}

	public async Task UpdateAsync(DeptEditModel model)
	{
		if (!model.DeptId.HasValue)
			throw new PmSoftException("部门ID不能为空");
		var sysDept = await deptRepository.GetAsync(model.DeptId.Value);
		if (sysDept == null)
			throw new PmSoftException("部门不存在");

		var dept = model.MapTo(sysDept, setter =>
		   {
			   setter.Ignore(dest => dest.Id);  // 忽略用户ID

		   });
		var areas = await areaService.ParseAreasAsync(model);
		dept.ProvinceNum = areas.Province?.Id;
		dept.CityNum = areas.City?.Id;
		dept.RegionNum = areas.County?.Id;
		await deptRepository.UpdateAsync(dept);
	}

	public async Task DeleteAsync(int deptId)
	{
		await deptRepository.DeleteByEntityIdAsync(deptId);
	}
}
